import Vue from 'vue'
import { setForm, getForm, setImgSrc, getImgSrc, setContent, getContent } from '@/utils/course'

const resetForm = {
  address: '',
  cPlan: '',
  cid: '',
  cost: '',
  costDesc: '',
  coverImg: '',
  createTime: '',
  discount: '',
  endDate: '',
  introduction: '',
  lastUpdateTime: '',
  precautions: '',
  startDate: '',
  stuNumber: '',
  tid: '',
  title: '',
  type: ''
}
const course = {
  state: {
    ruleForm: {
      address: '',
      cPlan: '',
      cid: '',
      cost: '',
      costDesc: '',
      coverImg: '',
      createTime: '',
      discount: '',
      endDate: '',
      introduction: '',
      lastUpdateTime: '',
      precautions: '',
      startDate: '',
      stuNumber: '',
      tid: '',
      title: '',
      type: ''
    }, // 表单内容
    fileList: [], // 图片存储列表
    imgSrc: '', // 图片路径
    content: '' // 课程介绍
  },

  mutations: {
    SET_RULE_FORM: (state, ruleForm) => {
      state.ruleForm = ruleForm
    },
    SET_RULE_FORM2: (state, ruleForm) => {
      const data = Object.assign({}, ruleForm);
      state.ruleForm = null;
      state.ruleForm = data
    },
    SET_COVERIMG: (state, object) => {
      const newObject = Object.assign({}, state.ruleForm, object);
      state.ruleForm = newObject
    },
    SET_IMGSRC: (state, imgSrc) => {
      state.imgSrc = imgSrc
    },
    SET_CONTENT: (state, content) => {
      state.content = content
    }
  },

  actions: {
    // 表单修改存入store
    AddlistenForm({ commit }, form) {
      return new Promise((resolve) => {
        commit('SET_RULE_FORM', form);
        setForm(form)
        resolve();
      })
    },

    // 表单修改图片地址
    addListenerCoverImg({ commit }, form) {
      return new Promise((resolve) => {
        commit('SET_COVERIMG', form);
        resolve();
      })
    },

    // imgSrc
    AddlistenImgSrc({ commit }, imgSrc) {
      return new Promise((resolve) => {
        commit('SET_IMGSRC', imgSrc);
        setImgSrc(imgSrc)
        resolve();
      })
    },

    resetFunc({ commit }) {
      return new Promise((resolve) => {
        commit('SET_CONTENT', '');
        commit('SET_IMGSRC', '');
        commit('SET_RULE_FORM2', resetForm);
        resolve();
      })
    }
  }
};

export default course;
